package com.sqx.modules.pushMsgType.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.postPush.entity.PostPush;
import com.sqx.modules.pushMsgType.entity.PushMsgType;
import com.sqx.modules.pushMsgType.dao.PushMsgTypeDao;
import com.sqx.modules.pushMsgType.service.PushMsgTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.sqx.common.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 工作提醒 服务实现类
 * @author wuChang
 * @since 2024-03-28
 */
@Service
public class PushMsgTypeServiceImp extends ServiceImpl<PushMsgTypeDao, PushMsgType> implements PushMsgTypeService {
    @Autowired
    private UserService userService;

    @Override
    public Result savePushMsgType(PushMsgType pushMsgType) {
        UserEntity userEntity = userService.getById(pushMsgType.getUserId());
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        if (pushMsgType.getTypeId() != null) {
            return Result.upStatus(baseMapper.updateById(pushMsgType));
        } else {
            pushMsgType.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(pushMsgType));
        }
    }

    @Override
    public IPage<PushMsgType> getPushMsgTypeList(Integer page, Integer limit, PushMsgType pushMsgType) {
        Page<PushMsgType> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.selectPage(pages, new QueryWrapper<>(pushMsgType).orderByAsc("sort"));
    }

    @Override
    public Result updateEnable(Long userId) {
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        PushMsgType pushMsgType = baseMapper.selectOne(new QueryWrapper<PushMsgType>().eq("user_id", userId));
        if (pushMsgType == null) {
            return Result.error("请先设置工作提醒");
        }
        if (pushMsgType.getIsEnable() == 1) {
            pushMsgType.setIsEnable(0);
        } else {
            if (StringUtils.isBlank(userEntity.getAccountOpenId())) {
                return Result.error("关注公众号可收到岗位提醒");
            }

            pushMsgType.setIsEnable(1);
        }
        return Result.upStatus(baseMapper.updateById(pushMsgType));
    }

    @Override
    public List<PushMsgType> getMsgListByPostPush(PostPush postPush, Double kilometre) {

        return baseMapper.getMsgListByPostPush(postPush, kilometre);
    }
}
